package com.txm.qqview;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MessagePage extends JFrame {
    private JTextPane chatPane; // 使用 JTextPane 替代 JTextArea
    private JTextField messageField;
    private JButton sendButton;
    private JFileChooser fileChooser;

    public MessagePage() {
        setTitle("微信聊天");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 聊天显示区域
        chatPane = new JTextPane(); // 使用 JTextPane
        chatPane.setEditable(false);
        JScrollPane chatScrollPane = new JScrollPane(chatPane);

        // 消息输入框
        messageField = new JTextField();
        messageField.addActionListener(e -> sendMessage());

        // 发送按钮
        sendButton = new JButton("发送");
        sendButton.addActionListener(e -> sendMessage());

        // 文件选择按钮
        JButton fileButton = new JButton("发送文件");
        fileButton.addActionListener(e -> sendFile());

        // 文件选择器
        fileChooser = new JFileChooser();

        // 布局管理
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(chatScrollPane, BorderLayout.CENTER);

        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new BorderLayout());
        inputPanel.add(messageField, BorderLayout.CENTER);
        inputPanel.add(fileButton, BorderLayout.WEST);
        inputPanel.add(sendButton, BorderLayout.EAST);

        panel.add(inputPanel, BorderLayout.SOUTH);

        add(panel);
    }

    private void sendMessage() {
        String message = messageField.getText();
        if (!message.isEmpty()) {
            appendChatMessage("你: " + message, Color.BLUE);
            messageField.setText("");
        }
    }

    private void sendFile() {
        int returnVal = fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            appendChatMessage("你: 发送了文件 " + file.getName(), Color.BLUE);
            // 这里添加文件发送的逻辑
        }
    }

    private void appendChatMessage(String message, Color color) {
        StyledDocument doc = chatPane.getStyledDocument(); // 使用 JTextPane 的 getStyledDocument 方法
        try {
            String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date());
            String chatMessage = timeStamp + " " + message + "\n";
            doc.insertString(doc.getLength(), chatMessage, null);

            SimpleAttributeSet sas = new SimpleAttributeSet();
            StyleConstants.setForeground(sas, color);
            int len = chatMessage.length();
            doc.setCharacterAttributes(doc.getLength() - len, len, sas, false);

            // 滚动到底部
            chatPane.setCaretPosition(doc.getLength());
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            MessagePage messagePage = new MessagePage();
            messagePage.setVisible(true);
        });
    }
}